<?php

/**
 * * * * * * * * * * * * * * * * *
 * HttpCurl Curl模拟Http工具类
 * * * * * * * * * * * * * * * * *
 */
namespace Curl;

class HttpCurl
{
    private $ch = null; // curl handle
    private $headers = array();// request header
    private $proxy = null; // http proxy
    private $timeout = 5;    // connnect timeout
    private $httpParams = null;


    public function __construct()
    {
        $this->ch = curl_init();
    }

    /**
     * * * * * * * * * * * * * * * * *
     * @notes:设置http header
     * @user: Bennent_G
     * @param $header
     * @return $this
     * @throws
     * * * * * * * * * * * * * * * * *
     */
    public function setHeader($header)
    {
        if (is_array($header)) {
            curl_setopt($this->ch, CURLOPT_HTTPHEADER, $header);
        }
        return $this;
    }

    /**
     * * * * * * * * * * * * * * * * *
     * @notes:设置http 超时
     * @user: Bennent_G
     * @param $time
     * @return $this
     * @throws
     * * * * * * * * * * * * * * * * *
     */
    public function setTimeout($time)
    {
        // 不能小于等于0
        if ($time <= 0) {
            $time = 5;
        }
        //只需要设置一个秒的数量就可以
        curl_setopt($this->ch, CURLOPT_TIMEOUT, $time);
        return $this;
    }

    /**
     * * * * * * * * * * * * * * * * *
     * @notes:设置http 代理
     * @user: Bennent_G
     * @param $proxy
     * @return $this
     * @throws
     * * * * * * * * * * * * * * * * *
     */
    public function setProxy($proxy)
    {
        if ($proxy) {
            curl_setopt($this->ch, CURLOPT_PROXY, $proxy);
        }
        return $this;
    }

    /**
     * * * * * * * * * * * * * * * * *
     * @notes:设置http 代理端口
     * @user: Bennent_G
     * @param $port
     * @return $this
     * @throws
     * * * * * * * * * * * * * * * * *
     */
    public function setProxyPort($port)
    {
        if (is_int($port)) {
            curl_setopt($this->ch, CURLOPT_PROXYPORT, $port);
        }
        return $this;
    }

    /**
     * * * * * * * * * * * * * * * * *
     * @notes:设置来源页面
     * @user: Bennent_G
     * @param string $referer
     * @return $this
     * @throws
     * * * * * * * * * * * * * * * * *
     */
    public function setReferer($referer = "")
    {
        if (!empty($referer))
            curl_setopt($this->ch, CURLOPT_REFERER, $referer);
        return $this;
    }

    /**
     * * * * * * * * * * * * * * * * *
     * @notes:设置用户代理
     * @user: Bennent_G
     * @param string $agent
     * @return $this
     * @throws
     * * * * * * * * * * * * * * * * *
     */
    public function setUserAgent($agent = "")
    {
        if ($agent) {
            // 模拟用户使用的浏览器
            curl_setopt($this->ch, CURLOPT_USERAGENT, $agent);
        }
        return $this;
    }

    /**
     * * * * * * * * * * * * * * * * *
     * @notes:http响应中是否显示header，1表示显示
     * @user: Bennent_G
     * @param $show
     * @return $this
     * @throws
     * * * * * * * * * * * * * * * * *
     */
    public function showResponseHeader($show)
    {
        curl_setopt($this->ch, CURLOPT_HEADER, $show);
        return $this;
    }

    /**
     * * * * * * * * * * * * * * * * *
     * @notes:设置http请求的参数,get或post
     * @user: Bennent_G
     * @param $params
     * @return $this
     * @throws
     * * * * * * * * * * * * * * * * *
     */
    public function setParams($params)
    {
        $this->httpParams = $params;
        return $this;
    }

    /**
     * * * * * * * * * * * * * * * * *
     * @notes:设置证书路径
     * @user: Bennent_G
     * @param $file
     * * * * * * * * * * * * * * * * *
     */
    public function setCainfo($file)
    {
        curl_setopt($this->ch, CURLOPT_CAINFO, $file);
    }

    /**
     * * * * * * * * * * * * * * * * *
     * @notes:模拟GET请求
     * @user: Bennent_G
     * @param $url
     * @param string $dataType text|json
     * @return mixed|bool|string
     * @throws
     * * * * * * * * * * * * * * * * *
     */
    public function get($url, $dataType = 'json')
    {
        if (stripos($url, 'https://') !== FALSE) {
            curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($this->ch, CURLOPT_SSLVERSION, 1);
        }
        // 设置get参数
        if (!empty($this->httpParams) && is_array($this->httpParams)) {
            if (strpos($url, '?') !== false) {
                $url .= http_build_query($this->httpParams);
            } else {
                $url .= '?'.http_build_query($this->httpParams);
            }
        }
        // end 设置get参数
        curl_setopt($this->ch, CURLOPT_URL, $url);
        curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);
        $content = curl_exec($this->ch);
        $status  = curl_getinfo($this->ch);
        curl_close($this->ch);
        if (isset($status['http_code']) && $status['http_code'] == 200) {
            if ($dataType == 'json') {
                $content = json_decode($content, true);
            }
            return $content;
        } else {
            return FALSE;
        }
    }

    /**
     * * * * * * * * * * * * * * * * *
     * @notes:模拟POST请求
     * @user: Bennent_G
     * @param $url
     * @param string $dataType
     * @return mixed|bool|string
     * @throws
     * * * * * * * * * * * * * * * * *
     */
    public function post($url, $dataType = 'json', $filePath = null)
    {
        if (stripos($url, 'https://') !== FALSE) {
            curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($this->ch, CURLOPT_SSLVERSION, 1);
        }
        curl_setopt($this->ch, CURLOPT_URL, $url);
        curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($this->ch, CURLOPT_POST, true);
        // 设置post body
        if (!empty($this->httpParams)) {
            if (is_array($this->httpParams)) {
                curl_setopt($this->ch, CURLOPT_POSTFIELDS, http_build_query($this->httpParams));
            }
        }
        if(!is_null($filePath)) {
            if(PHP_VERSION > '5.5.0') {
                curl_setopt($this->ch, CURLOPT_HEADER, false);
                $obj = new \CurlFile($filePath);
                $obj->setMimeType(mime_content_type($filePath));
                $post['file'] =  $obj;
            } else {
                curl_setopt($this->ch, CURLOPT_SAFE_UPLOAD, FALSE);
                $post['file'] = '@' . $filePath;
            }
            curl_setopt($this->ch, CURLOPT_POSTFIELDS, $post);
        }
        // end 设置post body
        $content = curl_exec($this->ch);
        $status  = curl_getinfo($this->ch);
        curl_close($this->ch);
        if (isset($status['http_code']) && $status['http_code'] == 200) {
            if ($dataType == 'json') {
                $content = json_decode($content, true);
            }
            return $content;
        } else {
            return FALSE;
        }
    }

    /**
     * * * * * * * * * * * * * * * * *
     * @notes:json数据post提交
     * @user: Bennent_G
     * @param $url
     * @param $data
     * @return false|mixed
     * @throws
     * * * * * * * * * * * * * * * * *
     */
    public function jsonPost($url, $data)
    {
        curl_setopt($this->ch, CURLOPT_URL, $url);
        curl_setopt($this->ch, CURLOPT_HEADER, false);
        curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($this->ch, CURLOPT_HTTPHEADER, array("Content-type: application/json"));
        curl_setopt($this->ch, CURLOPT_POST, true);
        curl_setopt($this->ch, CURLOPT_POSTFIELDS, $data);
        $content = curl_exec($this->ch);
        $status  = curl_getinfo($this->ch);
        curl_close($this->ch);
        if (isset($status['http_code']) && $status['http_code'] == 200) {
            return json_decode($content, true);
        } else {
            return FALSE;
        }
    }
}
